package com.ruoyi.common.utils;


import cn.hutool.core.lang.Assert;
import com.github.binarywang.wxpay.bean.order.WxPayAppOrderResult;
import com.github.binarywang.wxpay.bean.order.WxPayMwebOrderResult;
import com.github.binarywang.wxpay.bean.request.WxPayMicropayRequest;
import com.github.binarywang.wxpay.bean.request.WxPayRefundRequest;
import com.github.binarywang.wxpay.bean.request.WxPayUnifiedOrderRequest;
import com.github.binarywang.wxpay.bean.result.WxPayMicropayResult;
import com.github.binarywang.wxpay.bean.result.WxPayRefundResult;
import com.github.binarywang.wxpay.config.WxPayConfig;
import com.github.binarywang.wxpay.exception.WxPayException;
import com.github.binarywang.wxpay.service.WxPayService;
import com.github.binarywang.wxpay.service.impl.WxPayServiceImpl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Configuration;

import java.math.BigDecimal;

@Configuration
public class WeChatUtils {
    private static Logger logger = LoggerFactory.getLogger(WeChatUtils.class);


    //小程序
    public static final String appId = "wxfbb89868ec9e6a96";
    public static final String mchId = "1678228460";
    public static final String mchKey = "KEWlkj32yODQ2M7895457DQYJKoZIhvc";
    public static final String certPath = "classpath:cert/apiclient_cert.p12";


    /**
     * 微信支付相关配置
     */
    public static WxPayService getWxPayService() {
        WxPayConfig payConfig = new WxPayConfig();
        payConfig.setAppId(appId);
        payConfig.setMchId(mchId);
        payConfig.setMchKey(mchKey);
        payConfig.setKeyPath(certPath);
        payConfig.setTradeType("APP");
        payConfig.setSignType("MD5");
        WxPayService wxPayService = new WxPayServiceImpl();
        wxPayService.setConfig(payConfig);
        return wxPayService;
    }

    /**
     * 微信支付下单
     */
    public static WxPayMicropayResult micropay_app(String boby, String orderNo, BigDecimal payMoney, String authCode) {
        try {
            final WxPayService wxPayService = getWxPayService();
            //拉起支付
            WxPayMicropayRequest micropayRequest = WxPayMicropayRequest.newBuilder().body(boby)
                    .outTradeNo(orderNo)
                    .totalFee(payMoney.multiply(BigDecimal.valueOf(100)).intValue())
                    .spbillCreateIp("123.57.58.123")
                    .authCode(authCode)
                    .build();

            return wxPayService.micropay(micropayRequest);
        } catch (WxPayException e) {
            logger.error("微信下单异常", e.getErrCodeDes());
            Assert.isTrue(false, "微信下单失败");
            return null;
        }
    }

    /**
     * 微信支付下单
     */
    public static WxPayAppOrderResult weChatPay_app(String boby, String orderNo, BigDecimal payMoney, String notifyUrl) {
        try {
            final WxPayService wxPayService = getWxPayService();
            //拉起支付
            WxPayUnifiedOrderRequest wxPayUnifiedOrderRequest = WxPayUnifiedOrderRequest.newBuilder()
                    .body(boby)
                    .outTradeNo(orderNo)
                    .totalFee(payMoney.multiply(BigDecimal.valueOf(100)).intValue())
                    .spbillCreateIp("123.57.58.123")
                    .notifyUrl(notifyUrl)
                    .build();
            return wxPayService.createOrder(wxPayUnifiedOrderRequest);
        } catch (WxPayException e) {
            logger.error("微信下单异常", e.getErrCodeDes());
            Assert.isTrue(false, "微信下单失败");
            return null;
        }
    }

    public static Object weChatPay_jsapi(String boby, String orderNo, BigDecimal payMoney, String notifyUrl,String openId) {
        try {
            WxPayService wxPayService = getWxPayService();
            WxPayUnifiedOrderRequest wxPayUnifiedOrderRequest = WxPayUnifiedOrderRequest.newBuilder().body(boby).outTradeNo(orderNo).totalFee(Integer.valueOf(payMoney.multiply(BigDecimal.valueOf(100L)).intValue())).spbillCreateIp("123.57.58.123").tradeType("JSAPI").notifyUrl(notifyUrl).openid(openId).build();
            return wxPayService.createOrder(wxPayUnifiedOrderRequest);
        } catch (WxPayException e) {
            Assert.isTrue(false, "微信下单失败"+e.getMessage());
            return null;
        }
    }

    /**
     * 微信支付下单
     */
    public static WxPayRefundResult weChatPay_refund(String transactionId, String boby, String orderNo, String outRefundNo,BigDecimal orderMoney, BigDecimal payMoney, String notifyUrl) {
        try {
            final WxPayService wxPayService = getWxPayService();
            //拉起支付
            WxPayRefundRequest wxPayRefundRequest = WxPayRefundRequest.newBuilder()
                    .deviceInfo(boby)
                    .transactionId(transactionId)
                    .outTradeNo(orderNo)
                    .outRefundNo(outRefundNo)
                    .totalFee(orderMoney.multiply(BigDecimal.valueOf(100)).intValue())
                    .refundFee(payMoney.multiply(BigDecimal.valueOf(100)).intValue())
                    .build();
            return wxPayService.refund(wxPayRefundRequest);
        } catch (WxPayException e) {
            logger.error("微信下单异常", e.getErrCodeDes());
            Assert.isTrue(false, "微信下单失败");
            return null;
        }
    }
    public static WxPayMwebOrderResult weChatPay_h5(String boby, String orderNo, BigDecimal payMoney, String notifyUrl) {
        try {
            WxPayService wxPayService = getWxPayService();
            WxPayUnifiedOrderRequest wxPayUnifiedOrderRequest = WxPayUnifiedOrderRequest.newBuilder().body(boby).outTradeNo(orderNo).totalFee(Integer.valueOf(payMoney.multiply(BigDecimal.valueOf(100L)).intValue())).spbillCreateIp("123.57.58.123").tradeType("MWEB").notifyUrl(notifyUrl).build();
            return (WxPayMwebOrderResult)wxPayService.createOrder(wxPayUnifiedOrderRequest);
        } catch (WxPayException e) {
            Assert.isTrue(false, "微信下单失败");
            return null;
        }
    }
}
